package com.songshuai.webapi.crm.service.impl;

import com.songshuai.webapi.baseapi.dto.feign.response.DictionaryDto;
import com.songshuai.webapi.baseapi.dto.feign.response.MasterSourceItemDto;
import com.songshuai.webapi.baseapi.enumerations.DictionaryTypeEnum;
import com.songshuai.webapi.baseapi.enumerations.FollowStatusEnum;
import com.songshuai.webapi.baseapi.pojo.QueryResponse;
import com.songshuai.webapi.crm.feign.BaseDictionaryClient;
import com.songshuai.webapi.crm.feign.SchoolMasterSourceClient;
import com.songshuai.webapi.crm.service.DictionaryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.util.List;

@Service
@Slf4j
public class DictionaryServiceImpl implements DictionaryService {

    @Autowired
    private BaseDictionaryClient baseDictionaryClient;

    @Autowired
    private SchoolMasterSourceClient schoolMasterSourceClient;

    public Integer queryDictIdByNameAndType(String name,Integer typeId){
        if(StringUtils.isEmpty(name)||null==typeId){
            return null;
        }
        QueryResponse<List<DictionaryDto>> queryResponse=baseDictionaryClient.getDictionaryByType(typeId);
        if(null!=queryResponse){
            List<DictionaryDto> dictionaryDtoList=queryResponse.getData();
            for(DictionaryDto dictionaryDto:dictionaryDtoList){
                if(name.equalsIgnoreCase(dictionaryDto.getName())){
                    return dictionaryDto.getId();
                }
            }
        }
        return null;
    }


    public List<DictionaryDto> queryDictListByType(Integer typeId){
        if(null==typeId){
            return null;
        }
        QueryResponse<List<DictionaryDto>> queryResponse=baseDictionaryClient.getDictionaryByType(typeId);
        if(null!=queryResponse){
            List<DictionaryDto> dictionaryDtoList=queryResponse.getData();
            return dictionaryDtoList;
        }
        return null;
    }

    public Integer queryDictIdFromList(String name,List<DictionaryDto> dictionaryDtoList){
        if(StringUtils.isEmpty(name)|| CollectionUtils.isEmpty(dictionaryDtoList)){
            return null;
        }
        for(DictionaryDto dictionaryDto:dictionaryDtoList){
            if(name.equalsIgnoreCase(dictionaryDto.getName())){
                return dictionaryDto.getId();
            }
        }
        return null;
    }

   public Integer querySourceIdByName(String name){
        if(StringUtils.isEmpty(name)){
            return null;
        }
        QueryResponse<List<MasterSourceItemDto>> queryResponseSource=baseDictionaryClient.queryMasterSourceList();
        if(null!=queryResponseSource)
        if(null!=queryResponseSource){
            List<MasterSourceItemDto> masterSourceItemDtoList=queryResponseSource.getData();
            for(MasterSourceItemDto masterSourceItemDto:masterSourceItemDtoList){
                if(name.equalsIgnoreCase(masterSourceItemDto.getChannelName())){
                    return masterSourceItemDto.getSourceId();
                }
            }
        }
        return null;
    }

    public List<MasterSourceItemDto> querySourceList(){
        QueryResponse<List<MasterSourceItemDto>> queryResponseSource=schoolMasterSourceClient.queryMasterSourceList();
        if(null!=queryResponseSource)
        if(null!=queryResponseSource){
            List<MasterSourceItemDto> masterSourceItemDtoList=queryResponseSource.getData();
            return masterSourceItemDtoList;
        }
        return null;
    }

    public Integer querySourceIdFromList(String name,List<MasterSourceItemDto> sourceItemDtoList){
        if(StringUtils.isEmpty(name)||CollectionUtils.isEmpty(sourceItemDtoList)){
            return null;
        }
        for(MasterSourceItemDto sourceItemDto:sourceItemDtoList){
            if(name.equalsIgnoreCase(sourceItemDto.getChannelName())){
                return sourceItemDto.getSourceId();
            }
        }
        return null;
    }

}

